home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3.iso / chapte25 / ex21.c < prev    next >
C/C++ Source or Header  |  1995-04-24  |  2KB  |  70 lines

  1. #include <genstub.c>
  2.  
  3. #define OUR_EXCEPTION  0xE0000001
  4.  
  5. LONG WINAPI OurUnhandledExceptionFilter(LPEXCEPTION_POINTERS lpExceptionData)
  6. {
  7.    EXCEPTION_RECORD *ExceptionRecord = lpExceptionData->ExceptionRecord;
  8.    CONTEXT *ContextRecord = lpExceptionData->ContextRecord;
  9.    if (ExceptionRecord->ExceptionCode==OUR_EXCEPTION)
  10.    {
  11.       MessageBox( NULL, "Trapped raised exception", "Unhandled Exception Filter", MB_OK );
  12.       return EXCEPTION_EXECUTE_HANDLER;
  13.    }
  14.    else
  15.       return EXCEPTION_CONTINUE_SEARCH;
  16. }
  17.  
  18. LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  19. {
  20.    switch (uMsg)
  21.    {
  22.       case WM_COMMAND:
  23.          switch ( LOWORD( wParam )  )
  24.          {
  25.             case IDM_TEST:
  26.             {
  27.                char szBuffer[128];
  28.                HDC hDC = GetDC( hWnd );
  29.                // Set unhandled exception handler.
  30.                SetUnhandledExceptionFilter(OurUnhandledExceptionFilter);
  31.                // Raise exception try block.
  32.                try
  33.                {
  34.                   wsprintf( szBuffer, "Raising OUR_EXCEPTION first time" );
  35.                   MessageBox( hWnd, szBuffer, "Try Block", MB_OK );
  36.                   RaiseException(OUR_EXCEPTION, 0, 0, NULL);
  37.                }
  38.                except( GetExceptionCode() == OUR_EXCEPTION )
  39.                {
  40.                   wsprintf( szBuffer, "Trapped OUR_EXCEPTION in normal filter" );
  41.                   MessageBox( hWnd, szBuffer, "Except Block", MB_OK );
  42.                }
  43.                // Raise exception again using unhandled exception filter.
  44.                try
  45.                {
  46.                   wsprintf( szBuffer, "Raising OUR_EXCEPTION second time" );
  47.                   MessageBox( hWnd, szBuffer, "Try Block", MB_OK );
  48.                   RaiseException(OUR_EXCEPTION, 0, 0, NULL);
  49.                }
  50.                except (UnhandledExceptionFilter(GetExceptionInformation()))
  51.                {
  52.                   wsprintf( szBuffer, "Execution of except block for OUR_EXCEPTION." );
  53.                   MessageBox( hWnd, szBuffer, "Except Block", MB_OK );
  54.                }
  55.                ReleaseDC(hWnd, hDC);
  56.             }
  57.             break;
  58.             case IDM_EXIT:
  59.                 DestroyWindow(hWnd);
  60.             break;
  61.       }
  62.       break;
  63.       case WM_DESTROY:
  64.          PostQuitMessage(0);
  65.       break;
  66.       default:
  67.          return (DefWindowProc(hWnd, uMsg, wParam, lParam));
  68.    }
  69.    return (NULL);
  70. }